﻿<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>会话管理</title>
	<jsp:include page="/resources/inc/head.jsp" />
</head>
<body>
<div id="main">
	<div id="toolbar">
		<shiro:hasPermission name="upms:session:forceout">
			<a class="waves-effect waves-button" href="javascript:;" onclick="forceoutAction()">
				<i class="zmdi zmdi-run"></i> 强制退出</a>
		</shiro:hasPermission>
	</div>
	<table id="table"></table>
</div>
<jsp:include page="/resources/inc/footer.jsp" />
<script>
var $table = $('#table');
$(function() {
	// bootstrap table初始化
	$table.bootstrapTable({
		url: '${basePath}/manage/session/list',
		height: getHeight(),
		striped: true,
		search: false,
		showRefresh: true,
		showColumns: true,
		minimumCountColumns: 2,
		clickToSelect: true,
		detailView: true,
		detailFormatter: 'detailFormatter',
		pagination: true,
		paginationLoop: false,
		sidePagination: 'server',
		silentSort: false,
		smartDisplay: false,
		escape: true,
		searchOnEnterKey: true,
		idField: 'id',
		maintainSelected: true,
		toolbar: '#toolbar',
		columns: [
			{field: 'ck', checkbox: true},
			{field: 'id', title: '编号', sortable: true, align: 'center'},
			{field: 'startTimestamp', title: '创建时间', sortable: true, align: 'center', formatter: 'timeFormatter'},
			{field: 'lastAccessTime', title: '最后访问时间', formatter: 'timeFormatter'},
			{field: 'expired', title: '是否过期', align: 'center'},
			{field: 'host', title: '访问者IP', align: 'center'},
			{field: 'userAgent', title: '用户标识', align: 'center'},
			{field: 'status', title: '状态', align: 'center', formatter: 'statusFormatter'}
		]
	});
});
// 格式化状态
function statusFormatter(value, row, index) {
	if (value == 'on_line') {
		return '<span class="label label-success">在线</span>';
	}
	if (value == 'off_line') {
		return '<span class="label label-default">离线</span>';
	}
	if (value == 'force_logout') {
		return '<span class="label label-danger">踢离</span>';
	}
}
// 强制退出
function forceoutAction() {
	var rows = $table.bootstrapTable('getSelections');
	if (rows.length == 0) {
		layer.msg('请至少选择一条记录！');
	} else {
		var index = layer.confirm('确认强制退出该会话吗？', {
			btn: ['确定', '取消'] //按钮
		},function () {
			var ids = new Array();
			for (var i in rows) {
				ids.push(rows[i].id);
			}
			$.ajax({
				type: 'get',
				url: '${basePath}/manage/session/forceout/' + ids.join(","),
				success: function(result) {
					console.log(result);
					if (result.code != 1) {
						if (result.data instanceof Array) {
							$.each(result.data, function(index, value) {
								layer.msg(value.errorMsg,{icon:5});
							});
						} else {
							layer.msg(result.data.errorMsg,{icon:5});
						}
					} else {
						$table.bootstrapTable('refresh');
					}
					layer.close(index);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					layer.msg(textStatus,{icon: 5});
				}
			});
		},function () {

		});
	}
}
</script>
</body>
</html>